package com.qianfeng.es.index;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.qianfeng.es.utils.ESClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.json.JsonXContent;

import java.io.IOException;

/**
 * Created by jackiechan on 2021/10/29 15:08
 *
 * @author jackiechan
 * @version 1.0
 * //TODO 给最美丽的芳姐介绍学生
 * @since 1.0
 */

public class IndexService {


    public boolean isExist() throws IOException {
        RestHighLevelClient highLevelClient = ESClient.getClient();
        GetIndexRequest request = new GetIndexRequest("sms-logs-index");
        boolean exists = highLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        return exists;
    }



    public void createIndex() throws IOException {
        RestHighLevelClient highLevelClient = ESClient.getClient();
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("sms-logs-index");
        createIndexRequest.settings(
                Settings.builder()
                        .put("number_of_shards",5)
                        .put("number_of_replicas",1)
                        .build())
                        .mapping(
                                JsonXContent.contentBuilder()
                                        .startObject() //{
                                        .startObject("properties")//"properties":{
                                        .startObject("corpName").field("type","keyword").endObject()
                                        .startObject("createDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                                        .startObject("fee").field("type","integer").endObject()
                                        .startObject("ipAddr").field("type","ip").endObject()
                                        .startObject("longCode").field("type","keyword").endObject()
                                        .startObject("mobile").field("type","keyword").endObject()
                                        .startObject("operatorId").field("type","integer").endObject()
                                        .startObject("province").field("type","keyword").endObject()
                                        .startObject("replyTotal").field("type","integer").endObject()
                                        .startObject("state").field("type","integer").endObject()
                                        .startObject("sendDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                                        .startObject("smsContent").field("type","text").field("analyzer","ik_max_word").endObject()
                                        .endObject()
                                        .endObject());


        CreateIndexResponse response = highLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.err.println(response);

    }

}
